package mornd.test1;

import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor;
import org.springframework.context.annotation.CommonAnnotationBeanPostProcessor;
import org.springframework.context.annotation.ContextAnnotationAutowireCandidateResolver;
import org.springframework.context.support.GenericApplicationContext;

/**
 * @author: mornd
 * @dateTime: 2023/5/4 - 19:41
 */
public class App {
    public static void main(String[] args) {
        GenericApplicationContext context = new GenericApplicationContext();

        // 原始方法注册3个bean
        context.registerBean("bean1", Bean1.class);
        context.registerBean("bean2", Bean2.class);
        context.registerBean("bean3", Bean3.class);
        context.registerBean("bean4", Bean4.class);

        // 处理 bean 上面添加的 @Autowired @Value 等注解
        context.getDefaultListableBeanFactory().setAutowireCandidateResolver(
                        new ContextAnnotationAutowireCandidateResolver()); // 默认不能解析值类型，需添加该解析器
        context.registerBean(AutowiredAnnotationBeanPostProcessor.class);

        // 处理 bean 上面添加的 @Resource @PostConstruct @PreDestroy
        context.registerBean(CommonAnnotationBeanPostProcessor.class);

        // 处理 bean 上面添加的 @ConfigurationProperties
        ConfigurationPropertiesBindingPostProcessor.register(context.getDefaultListableBeanFactory());

        // 执行 beanFactory 后置处理器，添加 bean 后处理器，初始化所有单例bean
        context.refresh();

        String[] names = context.getBeanDefinitionNames();
        for (String name : names) {
            System.out.println("==" + name);
        }

        Bean4 bean4 = context.getBean(Bean4.class);
        System.out.println(bean4);

        context.close();
    }
}
